import 'package:flutter/material.dart';
import 'package:lx_widgets_library/custom_view/refresh_view.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

// 便捷的下拉刷新和上拉加载更多组件封装
// 【切记】不要在LxRefreshView的外层再包一层Padding，否则会导致刷新功能异常
class LxRefreshView extends StatefulWidget {
  final Future<void> Function() onRefresh; // 刷新回调
  final Future<void> Function() onLoadMore; // 加载更多回调
  final Widget child; // 刷新内容组件
  final bool enablePullUp; // 是否启用上拉加载更多
  final bool enablePullDown; // 是否启用下拉刷新
  final bool autoRefreshOnce; // 是否在初始化后自动触发一次刷新
  final bool noMoreData; // 由外层控制是否没有更多数据

  const LxRefreshView({
    Key? key,
    required this.onRefresh,
    required this.onLoadMore,
    required this.child,
    this.enablePullUp = true,
    this.enablePullDown = true,
    this.autoRefreshOnce = true,
    this.noMoreData = false, // 默认有更多数据
  }) : super(key: key);

  // 便捷方法：手动触发刷新
  static Future<void> triggerRefresh(BuildContext context) async {
    final state = context.findAncestorStateOfType<_LxRefreshViewState>();
    state?._refreshController.requestRefresh();
  }

  // 便捷方法：手动触发加载
  static void triggerLoad(BuildContext context) {
    final state = context.findAncestorStateOfType<_LxRefreshViewState>();
    state?._refreshController.requestLoading();
  }

  // 便捷方法：设置没有更多数据
  static void setNoMoreData(BuildContext context, bool noMore) {
    final state = context.findAncestorStateOfType<_LxRefreshViewState>();
    if (noMore) {
      state?._refreshController.loadNoData();
    } else {
      state?._refreshController.resetNoData();
    }
  }

  @override
  State<StatefulWidget> createState() => _LxRefreshViewState();
}

class _LxRefreshViewState extends State<LxRefreshView> {
  late RefreshController _refreshController;

  @override
  void initState() {
    super.initState();
    _refreshController = RefreshController();

    if (widget.autoRefreshOnce) {}
  }

  @override
  void didUpdateWidget(LxRefreshView oldWidget) {
    super.didUpdateWidget(oldWidget);

    // 当 noMoreData 属性变化时，更新刷新控件的状态
    if (oldWidget.noMoreData != widget.noMoreData) {
      if (widget.noMoreData) {
        _refreshController.loadNoData();
      } else {
        _refreshController.resetNoData();
      }
    }
  }

  @override
  void dispose() {
    _refreshController.dispose();
    super.dispose();
  }

  // 处理刷新逻辑
  Future<void> _handleRefresh() async {
    try {
      await widget
          .onRefresh()
          .then((_) => Future.delayed(const Duration(milliseconds: 300)));
      _refreshController.refreshCompleted();
      _refreshController.resetNoData();
    } catch (e) {
      _refreshController.refreshFailed();
    }
  }

  // 处理加载更多逻辑
  Future<void> _handleLoadMore() async {
    try {
      debugPrint("handle load more called");
      await widget.onLoadMore();

      // 加载完成后，根据外层传入的 noMoreData 决定状态
      if (widget.noMoreData) {
        _refreshController.loadNoData();
      } else {
        _refreshController.loadComplete();
      }
    } catch (e) {
      _refreshController.loadFailed();
    }
  }

  @override
  Widget build(BuildContext context) {
    return RefreshView(
      refreshController: _refreshController,
      onRefresh: _handleRefresh,
      onLoad: _handleLoadMore,
      enablePullUp: widget.enablePullUp,
      enablePullDown: widget.enablePullDown,
      autoRefreshOnce: widget.autoRefreshOnce,
      child: widget.child,
    );
  }
}
